<?php
/*
	Copyright © Eleanor CMS
	URL: http://eleanor-cms.ru, http://eleanor-cms.com
	E-mail: support@eleanor-cms.ru
	Developing: Alexander Sunvas*
	Interface: Rumin Sergey
	=====
	*Pseudonym
*/
class ApiMenu extends BaseClass
{
	private
		$config=array();

	public function __construct($config=array())
	{
		$this->config=$config;
	}

	public function GetOrderedList($lang=false,$status=1)
	{
		if(!$lang)
			$lang=Language::$main;

		if($db=Eleanor::$Cache->Get($this->config['n'] .'_dump'. $status .'_'. $lang))
			return $db;

		$maxlen=0;
		$res=$to1sort=$to2sort=$db=array();
		$R = Eleanor::$Db->Query('SELECT `id`,`url`,`eval_url`,`title`,`pos`,`parents`,`status` FROM `'.$this->config['t'].'` LEFT JOIN `'.$this->config['tl'].'` USING(`id`) WHERE `language` IN (\'\',\''.$lang.'\')'.($status===false ? '' : ' AND `status`=1'));
		while($a=$R->fetch_assoc())
		{
			if($a['parents'])
			{
				$cnt=substr_count($a['parents'],',');
				$to1sort[$a['id']]=$cnt;
				$maxlen=max($cnt,$maxlen);
			}
			$db[$a['id']]=$a;
			$to2sort[$a['id']]=$a['pos'];
		}
		asort($to1sort,SORT_NUMERIC);

		foreach($to1sort as $k=>&$v)
			if($db[$k]['parents'] and preg_match('#(\d+),$#',$db[$k]['parents'],$p)>0)
				if(isset($to2sort[$p[1]]))
					$to2sort[$k]=$to2sort[$p[1]].','.$to2sort[$k];
				else
					unset($to1sort[$k],$db[$k],$to2sort[$k]);

		foreach($to2sort as $k=>&$v)
			$v.=str_repeat(',0',$maxlen-substr_count($db[$k]['parents'],','));

		natsort($to2sort);
		foreach($to2sort as $k=>&$v)
			$res[(int)$db[$k]['id']]=$db[$k];
		Eleanor::$Cache->Put($this->config['n'].'_dump'.$status.'_'.$lang,$res,86400);
		return $res;
	}

	private static $level_sub = 0;
	public static function BuildMultilineMenu($a, $first='<ul>') {
		self::$level_sub++;
		$parents = reset($a);
		$l = strlen($parents['parents']);
		$c = $first;
		$n = -1;
		$onp = false;
		foreach($a as &$v) {
			++$n;
			$nl=strlen($v['parents']);
			if($nl != $l) {
				if($l>$nl)
					break;
				elseif(!$onp) {
					if (self::$level_sub > 1)
						$c .= self::BuildMultilineMenu(array_slice($a, $n), '<ul class="drop_down_menu_submenu">');
					else
						$c .= self::BuildMultilineMenu(array_slice($a, $n), '<ul class="drop_down_menu">');
					self::$level_sub--;
					$onp = true;
				}
				continue;
			}
			if ($n>0 && self::$level_sub > 1) {
				$c .= '</li><li class="drop_down_menu_border"></li>';
			}
			elseif($n > 0)
				$c .= '</li>';

				if (static::$level_sub === 1)
					$c .= '<li class="main_menu_item main_menu_hover"><a href="'. $v['url'] .'"'. $v['params'] .' title="'. htmlspecialchars(['title']) .'">'. $v['title'] .'</a>';
			elseif (self::$level_sub > 1)
				$c .= '<li class="drop_down_menu_item"><a href="'. $v['url'] .'"'. $v['params'] .' title="'. htmlspecialchars($v['title']) .'">'. $v['title'] .'</a>';
			$onp = false;
		}if (self::$level_sub === 1) {
			return $c .'</li>
				<li id="main_menu_right" class="main_menu_item"></li></ul>';
		} else
			return $c .'</li></ul>';
	}

	public function GetModules() {
		$R = Eleanor::$Db->Query('
			SELECT `id`, `title_l`, `active`, `sections`, `path`
			FROM `'. P .'modules`
			ORDER BY `id`
		');
		while($a = $R->fetch_assoc()) {
			$modules[$a['id']]['title'] = Eleanor::FilterLangValues(unserialize($a['title_l']));
			$modules[$a['id']]['active'] = $a['active'];
			$modules[$a['id']]['sections'] = unserialize($a['sections']);
			$modules[$a['id']]['path'] = $a['path'];
		}
		return $modules;
	}
}